//
//  ST_PairsLoveFilterViewController.swift
//  btvideo
//
//  Created by mac on 2020/5/6.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import MultiSlider

//喜欢筛选
class ST_PairsLoveFilterViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        view.backgroundColor = .clear
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(20)
            make.height.equalTo(375+40+20+BTSafeAreaBottom)
        }
        
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        
        
        root_view.addSubview(ageItem)
        ageItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(92)
        }
        
        
        root_view.addSubview(seekItem)
        seekItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(ageItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        
        root_view.addSubview(face_detected_item)
        face_detected_item.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(seekItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        root_view.addSubview(have_my_love_item)
        have_my_love_item.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(face_detected_item.snp.bottom)
            make.height.equalTo(60)
        }
        
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20-20-BTSafeAreaBottom)
            make.width.equalTo(260)
            make.height.equalTo(44)
        }
        
        bing_default_filter()
    }
    
    var current_filter = ST_PinterestHomeFilterModel()
    
    
    
    func bing_default_filter()  {
        
        face_detected_item.bind(model: self.current_filter.faceDetected)
        
        have_my_love_item.bind(model: self.current_filter.haveMyloved)
        
        ageItem.bind(option: self.current_filter.age)
    
        seekItem.bind(model: self.current_filter.seek)
        
    }
    
    
//    MARK: - Properties
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
       let control = UILabel()
        let attrString = NSMutableAttributedString(string: "只看符合我要求的")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        return control
    }()
    
    
    /*
    fileprivate lazy var age_slider:MultiSlider = {
        
        let control = MultiSlider()
        control.minimumValue = minimum_age
        control.maximumValue = maximum_age
        control.isVertical = false
        control.tintColor = UIColor.init(hex: 0xFEC207)
        control.outerTrackColor = UIColor.init(hex: 0xF5F8FA)
        control.value = [age_slider,end_age]
        control.thumbImage = ST_ImageUtils.imageName("IndexItem_icon_选择")
        control.showsThumbImageShadow = false
        control.addTarget(self, action: #selector(sliderChanged(slider:)), for: UIControl.Event.valueChanged)
        return control
    }()
    
    fileprivate var age_slider = 18.cgFloat
    
    fileprivate var end_age = 50.cgFloat
    
    fileprivate var minimum_age = 18.cgFloat
    
    fileprivate var maximum_age = 50.cgFloat
    
    fileprivate lazy var ageKeyLabel:UILabel = {
       let control = UILabel()
        let attrString = NSMutableAttributedString(string: "年龄")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        return control
    }()
    
    fileprivate lazy var ageValueLabel:UILabel = {
       let control = UILabel()
        control.textColor = UIColor(red: 0.64, green: 0.64, blue: 0.64, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 14)
        control.text = "\(age_slider.int)岁-\(end_age.int)岁"
        return control
    }()
    
    
    //
    fileprivate lazy var ownPeculiarityLabel:UILabel = {
       let control = UILabel()
        let attrString = NSMutableAttributedString(string: "只看真人用户")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        return control
    }()
    
    fileprivate lazy var ownPeculiaritySwitch:UISwitch = {
       let control = UISwitch()
        control.onTintColor = UIColor.init(hex: 0xFEC207)
        control.backgroundColor = UIColor.init(hex: 0xACB4C0)
        control.cornerRadius = 16
        return control
    }()
    
    
    fileprivate lazy var ownMyLovePeculiarityLabel:UILabel = {
       let control = UILabel()
        let attrString = NSMutableAttributedString(string: "对方有我喜欢的特质")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        return control
    }()
    
    
    fileprivate lazy var ownMyLovePeculiaritySwitch:UISwitch = {
       let control = UISwitch()
        control.onTintColor = UIColor.init(hex: 0xFEC207)
        control.backgroundColor = UIColor.init(hex: 0xACB4C0)
        control.cornerRadius = 16
        return control
    }()
    */
    
    
    fileprivate lazy var ageItem:ST_FilterRangeSliderItemView = {
        
        let control = ST_FilterRangeSliderItemView()
        control.didChange = {[weak self] min,max in
           
            self?.current_filter.age.minValue = min
            self?.current_filter.age.maxValue = max
            
        }
        return control
    }()
    
    fileprivate lazy var face_detected_item:PinterestFilterSwitchControl = {
        let control = PinterestFilterSwitchControl()
        control.pushOptionClosure = {[weak self] value in
            self?.current_filter.faceDetected = value
            self?.bing_default_filter()
        }
        return control
    }()
    
    fileprivate lazy var have_my_love_item:PinterestFilterSwitchControl = {
        let control = PinterestFilterSwitchControl()
        control.pushOptionClosure = {[weak self] value in
            self?.current_filter.haveMyloved = value
            self?.bing_default_filter()
        }
        return control
    }()
    
    fileprivate lazy var seekItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        control.pushOptionClosure = {[weak self] m in
            guard let strongSelf = self else{
                return
            }
            
            let optionsController = ST_PinterestHomeFilterOptionsController()
            
            optionsController.last_selected_option = m
            
            optionsController.selected_closure = { selected in
                
                strongSelf.current_filter.seek = selected
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    
    fileprivate lazy var confirm_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 35/2.0
        control.setTitleForAllStates("确定")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    var confirm_closure:((ST_PinterestHomeFilterModel)->())?
}

//MARK: - Event

extension ST_PairsLoveFilterViewController{
    
    
    
    
    @objc func cancel_handle(){
        
        
        self.goback_previous_controller()
    }
    
    @objc func confirm_handle(){
        
        /*
        lastFilter.age_start = age_slider.int
        lastFilter.age_end = end_age.int
        lastFilter.faceIdentified = ownPeculiaritySwitch.isOn ? 1 : 0
        lastFilter.seek = self.seekItem.model
        lastFilter.have_like_feature = ownMyLovePeculiaritySwitch.isOn ? 1 : 0
        self.confirm_closure?(lastFilter)
        let param = self.current_filter.conver_to_param()
        refresh_closure?(param)
        */
        
        
        self.confirm_closure?(self.current_filter)
        goback_previous_controller()
    }
    
}


//MARK: - Model

class ST_PairsLoveFilterModel: NSObject {
    var age_start = 18
    var age_end = 50
    var faceIdentified = 0
    var seek:PinterestFilterOptionModel?
    var have_feature = 0
    var have_like_feature = 0
}


